package org.zjt.immutable;

import sun.plugin.javascript.navig.Array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Description:
 *      一般返回值都要是不可变的。
 *      final collection 不可变，需要用不可变集合 或者 每次返回拷贝来解决。
 *
 * @author juntao.zhang
 * Date: 2018-06-05 下午2:49
 * @see
 */
public class ImmutableList {

    private static final List<String> DATA = Collections.unmodifiableList(Arrays.asList("https://www.baidu.com","https://www.google.com","https://www.github.com"));

    private static final List<String> DATA2 = Arrays.asList("https://www.baidu.com","https://www.google.com","https://www.github.com");


    public static List<String> getDATA() {
        return DATA;
    }

    public static List<String> getDATA2() {
        return Collections.unmodifiableList(DATA2);
    }


    public static void main(String[] args) {

        // 对象操作加锁。同步的list
        List<String> list = new ArrayList<>();
        list.add("22222");
        List<String> strings = Collections.synchronizedList(list);


        // 得到immutable list
        List<String> data = getDATA();
        data.clear();


        // 得到immutable list
        List<String> data2 = getDATA2();
        data2.clear();

    }
}
